package com.example.test.chapter10.contextmenu;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.example.test.R;
import com.example.test.chapter10.MainChapter10;

public class ContextMenu2Activity extends Activity implements
		OnItemClickListener {
	TextView selection;
	ListView listView;
	String[] items = { "HTC Rezound", "Samsung Galaxy S II Skyrocket",
			"Samsung Galaxy Nexus", "Motorola Droid Razr", "Samsung Galaxy S",
			"Samsung Epic Touch 4G", "iPhone 4S", "HTC Titan" };
	public static final int EIGHT_ID = Menu.FIRST + 1;
	public static final int SIXTEEN_ID = Menu.FIRST + 2;
	public static final int TWENTY_FOUR_ID = Menu.FIRST + 3;
	public static final int TWO_ID = Menu.FIRST + 4;
	public static final int THIRTY_TWO_ID = Menu.FIRST + 5;
	public static final int FORTY_ID = Menu.FIRST + 6;
	public static final int ONE_ID = Menu.FIRST + 7;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.maincontextmenu1);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, items);
		selection = (TextView) findViewById(R.id.selection_menu_context_1);
		listView = (ListView) findViewById(R.id.list_menu_context_1);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(this);
		registerForContextMenu(listView);
		Button btn = (Button) findViewById(R.id.backpage);
		btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getApplicationContext(),
						MainChapter10.class);
				startActivity(intent);
				finish();
			}
		});
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		selection.setText(items[arg2]);
	}

	private void populateMenu(Menu menu) {
		menu.add(Menu.NONE, ONE_ID, Menu.NONE, "1 Pixel");
		menu.add(Menu.NONE, TWO_ID, Menu.NONE, "2 Pixels");
		menu.add(Menu.NONE, EIGHT_ID, Menu.NONE, "8 Pixels");
		menu.add(Menu.NONE, SIXTEEN_ID, Menu.NONE, "16 Pixels");
		menu.add(Menu.NONE, TWENTY_FOUR_ID, Menu.NONE, "24 Pixels");
		menu.add(Menu.NONE, THIRTY_TWO_ID, Menu.NONE, "32 Pixels");
		menu.add(Menu.NONE, FORTY_ID, Menu.NONE, "40 Pixels");
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		populateMenu(menu);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		populateMenu(menu);
		return super.onCreateOptionsMenu(menu);
	}

	private boolean applyMenuChoice(MenuItem item) {
		switch (item.getItemId()) {
		case ONE_ID:
			listView.setDividerHeight(1);
			return (true);
		case EIGHT_ID:
			listView.setDividerHeight(8);
			return (true);
		case SIXTEEN_ID:
			listView.setDividerHeight(16);
			return (true);
		case TWENTY_FOUR_ID:
			listView.setDividerHeight(24);
			return (true);
		case TWO_ID:
			listView.setDividerHeight(2);
			return (true);
		case THIRTY_TWO_ID:
			listView.setDividerHeight(32);
			return (true);
		case FORTY_ID:
			listView.setDividerHeight(40);
			return (true);
		}
		return (false);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		return applyMenuChoice(item) || super.onOptionsItemSelected(item);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		return applyMenuChoice(item) || super.onContextItemSelected(item);
	}
}
